<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use OSS\OssClient;

class UploadController extends Controller
{
    /**
     * 上传文件
     * @param $fileObj
     * @param $remoteDir
     * @return array
     */
    public function doUpload($fileObj, $remoteDir)
    {
        $path = $fileObj->store($remoteDir, 'oss');
        if (!$path) {
            return ["status" => 0, "fileUrl" => ""];
        }
        $imgUrl = "https://" . config("filesystems.disks.oss.bucket") . "." . config("filesystems.disks.oss.endpoint") . '/' . $path;
        return ["status" => 1, "fileUrl" => $imgUrl];
    }

    //删除文件
    public function deleteImg($imgUrl)
    {
        $path = str_replace("http://", "", "$imgUrl");
        $pos = strpos($path, "/");
        $imgUrl = substr($path, $pos + 1);
        $isExist = Storage::exists($imgUrl);
        if (!$isExist) {
            return true;
        }
        $res = Storage::delete($imgUrl);
        if (!$res) {
            return false;
        }
        return true;

    }

    function uploadImageCommon($dst, $getFile)
    {
        #配置OSS基本配置
        $ossConfig = config("filesystems.disks.oss");
        $config = array(
            'KeyId' => $ossConfig['access_id'],
            'KeySecret' => $ossConfig['access_key'],
            'Endpoint' => $ossConfig['endpoint'],
            'Bucket' => $ossConfig['bucket']
        );
        $ossClient = new OssClient($config['KeyId'], $config['KeySecret'],
            $config['Endpoint']);
        #执行阿里云上传
        $result = $ossClient->uploadFile($config['Bucket'], $dst, $getFile);
        #返回
        return $result['info'];
    }

    public function uploadImg(Request $request)
    {
        $file = $request->file("mypic");
        // dd($file);
        if (!empty($file)) {
            foreach ($file as $key => $value) {
                $len = $key;
            }
            if ($len > 25) {
                return response()->json(['ResultData' => 6, 'info' => '最多可以上传25张图片']);
            }
            $m = 0;
            $k = 0;
            for ($i = 0; $i <= $len; $i++) {
                // $n 表示第几张图片
                $n = $i + 1;
                if ($file[$i]->isValid()) {
                    if (in_array(strtolower($file[$i]->extension()), ['jpeg', 'jpg', 'gif', 'gpeg', 'png'])) {
                        $picname = $file[$i]->getClientOriginalName();//获取上传原文件名
                        $ext = $file[$i]->getClientOriginalExtension();//获取上传文件的后缀名
                        // 重命名
                        $filename = time() . str_random(6) . "." . $ext;
                        if ($file[$i]->move("uploads/images", $filename)) {
                            $newFileName = '/' . "uploads/images" . '/' . $filename;
                            $m = $m + 1;
                            // return response()->json(['ResultData' => 0, 'info' => '上传成功', 'newFileName' => $newFileName ]);
                        } else {
                            $k = $k + 1;
                            // return response()->json(['ResultData' => 4, 'info' => '上传失败']);
                        }
                        $msg = $m . "张图片上传成功 " . $k . "张图片上传失败<br>";
                        $return[] = ['ResultData' => 0, 'info' => $msg, 'newFileName' => $newFileName];
                    } else {
                        return response()->json(['ResultData' => 3, 'info' => '第' . $n . '张图片后缀名不合法!<br/>' . '只支持jpeg/jpg/png/gif格式']);
                    }
                } else {
                    return response()->json(['ResultData' => 1, 'info' => '第' . $n . '张图片超过最大限制!<br/>' . '图片最大支持2M']);
                }
            }
        } else {
            return response()->json(['ResultData' => 5, 'info' => '请选择文件']);
        }
        return $return;
    }
}
